/**
 * Sensor Network Development Kit for 1-Wire Network (SeN)
 * Version : 0.1
 * Revision date : 2008-10-13
 * Author: Dubravko Penezic, dpenezic@gmail.com
 * 
 */

package utilityLib;

/**
 * MemoryDataTransformer class implement method to transform data for memory in Maxim-Dallas chip
 * Version : 0.1.1
 * Revision date : 2008-11-01
 * 
 * @author Dubravko Penezic, dpenezic@gmail.com
 *
 */

public class MemoryDataTransformer {

	public MemoryDataTransformer(){

	}

	/**
	 * byteArrayToHexString method convert byte array in hex string (without spaces and any other character)
	 * 
	 * @param value
	 * @return
	 */
	public String byteArrayToHexString(byte[] value){

		String st = "";

		for (int i=0; i<value.length; i++){
			st += Integer.toString( ( value[i] & 0xff ) + 0x100, 16).substring( 1 );
		}

		return st;
	}

	/**
	 * hexStringToByteArray method convert hex string (without spaces and any other character) to byte array
	 * @param value
	 * @return
	 */
	public byte[] hexStringToByteArray(String value){

		// allocate byte array based on half of string length
		int numBytes = (value.length()) / 2;
		byte[] rv = new byte[numBytes];

		// loop through the string - 2 bytes at a time converting it to decimal equivalent and store in byte array
		// x variable used to hold byte array element position
		for(int i=0; i<numBytes; i++)
		{
			int j = Integer.parseInt(value.substring(i,i+2), 16);
			rv[i] = (byte) (j & 0x000000ff);
		}

		// return the finished byte array of decimal values
		return rv;
	}

	/**
	 * counterByteArrayToLong method convert byte array to 32 bit unsigned counter (DS chip)
	 * @param value
	 * @return
	 */
	public long counterByteArrayToLong(byte[] value){

		long ch1 = value[0] & 0xff;
		long ch2 = value[1] & 0xff;
		long ch3 = value[2] & 0xff;
		long ch4 = value[3] & 0xff;

		return (ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0);
	}

	/**
	 * counterLongToByteArray method convert 32 bit unsigned counter (DS chip) to byte array
	 * @param value
	 * @return
	 */
	public byte[] counterLongToByteArray(long value){
		byte[] rv = new byte[4];

		rv[0] = (byte)((value >>> 24) & 0xFF);
		rv[1] = (byte)((value >>> 16) & 0xFF);
		rv[2] = (byte)((value >>> 8) & 0xFF);
		rv[3] = (byte)((value >>> 0) & 0xFF);

		return rv;
	}

	/**
	 * byteArrayToLong method convert byte array to long
	 * 
	 * @param value
	 * @return
	 */
	public long byteArrayToLong(byte[] value){
		long ch1 = value[0] & 0xff;
		long ch2 = value[1] & 0xff;
		long ch3 = value[2] & 0xff;
		long ch4 = value[3] & 0xff;
		long ch5 = value[4] & 0xff;
		long ch6 = value[5] & 0xff;
		long ch7 = value[6] & 0xff;
		long ch8 = value[7] & 0xff;
		long rv = ((ch1 << 56) + (ch2 << 48) + (ch3 << 40) + (ch4 << 32) + 
				(ch5 << 24) + (ch6 << 16) + (ch7 << 8) + (ch8 << 0));
		if (rv<0) rv++;

		return rv;
	}

	/**
	 * longToByteArray convert long to byte array
	 * 
	 * @param value
	 * @return
	 */
	public byte[] longToByteArray(long value){
		byte [] out =new byte [8];

		out[0]=(byte)((value >>> 56) & 0xFF);
		out[1]=(byte)((value >>> 48) & 0xFF);
		out[2]=(byte)((value >>> 40) & 0xFF);
		out[3]=(byte)((value >>> 32) & 0xFF);
		out[4]=(byte)((value >>> 24) & 0xFF);
		out[5]=(byte)((value >>> 16) & 0xFF);
		out[6]=(byte)((value >>>  8) & 0xFF);
		out[7]=(byte)((value >>>  0) & 0xFF);

		return out;

	}

	/**
	 * byteArrayToInt convert byte array to int
	 * 
	 * @param value
	 * @return
	 */
	public int byteArrayToInt(byte[] value){
		int ch1 = value[0] & 0xff;
		int ch2 = value[1] & 0xff;
		int ch3 = value[2] & 0xff;
		int ch4 = value[3] & 0xff;

		int rv = ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
		if (rv<0) rv++;

		return rv;
	}

	/**
	 * intToByteArray method convert int to byte array
	 * @param value
	 * @return
	 */
	public byte[] intToByteArray(int value){
		byte [] out =new byte [4];

		out[0]=(byte)((value >>> 24) & 0xFF);
		out[1]=(byte)((value >>> 16) & 0xFF);
		out[2]=(byte)((value >>>  8) & 0xFF);
		out[3]=(byte)((value >>>  0) & 0xFF);

		return out;

	}


	/**
	 * byteArrayToShort method convert byte array to short
	 * @param value
	 * @return
	 */
	public short byteArrayToShort(byte[] value){
		int ch1 = value[0] & 0xff;
		int ch2 = value[1] & 0xff;

		short rv = (short) ((ch1 << 8) + (ch2 << 0));
		if (rv<0) rv++;

		return rv;
	}


	/**
	 * shortToByteArea method transform short to byte array
	 * 
	 * @param value
	 * @return
	 */
	public byte[] shortToByteArea(short value){
		byte [] out =new byte [4];

		out[0]=(byte)((value >>>  8) & 0xFF);
		out[1]=(byte)((value >>>  0) & 0xFF);

		return out;
	}


	/**
	 * byteArrayToString method convert byte array to string	  
	 * @param value
	 * @return
	 */
	public String byteArrayToString(byte[] value){
		String rv = new String(value);

		return rv;
	}


	/**
	 * stringToByteArray method convert string to byte array
	 * @param value
	 * @return
	 */
	public byte[] stringToByteArray(String value){

		return value.getBytes();		  
	}


}
